如何將帖子從 Wordpress 導入 Wagtail 2(Draftail 編輯器),包括圖像? (How to import posts from Wordpress to Wagtail 2 (Draftail editor) including images?)


問題描述

如何將帖子從 Wordpress 導入 Wagtail 2(Draftail 編輯器),包括圖像? (How to import posts from Wordpress to Wagtail 2 (Draftail editor) including images?)

我正在嘗試將帖子從 Wordpress 導入 Wagtail,包括圖像。

我意識到 Draftail 編輯器使用專用標籤保存圖像這一事實,例如:

<embed alt="fonctionnement‑pim‑schema‑640.png" embedtype="image" format="fullwidth" id="412" />

當我從 Wordpress 導入我的帖子時,我將 img 標籤轉換為嵌入標籤。下面是我的代碼片段:

resp = requests.get(img["src"], stream=True)
if resp.status_code != requests.codes.ok:
    print("Unable to import " + img["src"])
    continue
fp = BytesIO()
fp.write(resp.content)
image = Image(title=file_name, width=width, height=height)
image.file.save(file_name, File(fp))
image.save()
try:
    embed_id = image.get_rendition("original").id
    embed_alt = image.get_rendition("original").alt
    new_tag = soup.new_tag('embed', alt=f'{embed_alt}', embedtype="image", format="fullwidth", id=f'{embed_id}')
    img.replace_with(new_tag)

它似乎有效。當我檢查數據庫時,所有 img 標記都被替換為格式正確的嵌入標記,並且所有圖像都被下載到媒體文件夾中。

不幸的是,當我檢查管理區域時。嵌入標籤存在但無法識別圖片:






<hr>




<h2>參考解法</h2>

<h4>方法 1:</h4> <p>I found what was causing the issue.</p>
<p>I was using a rendition image to fetch the id:</p>
<pre><code>embed_id = image.get_rendition("original").id
</code></pre>
<p>But rendition is for templates. In Draftail, we need to use the Image object id and not a rendition image id:</p>
<pre><code>embed_id = image.id
</code></pre>
<p>For <code>alt</code> text, it's your call to choose something from your initial content.</p><p>(by <a href=RedjamRedjam)

參考文件

  1. How to import posts from Wordpress to Wagtail 2 (Draftail editor) including images? (CC BY‑SA 2.5/3.0/4.0)

#wagtail #Python #Django #wordpress






相關問題

Wagtail Cms 是否支持 Google 登錄和用戶登錄添加會話 (Does Wagtail Cms support Google login and user login add session to)

Wagtail Django-form編輯現有對象 (Wagtail Django-form edit existing object)

如何將 Wagtail 'admin' 菜單添加到自定義模板? (How to add Wagtail 'admin' menu to custom templates?)

django.db.utils.OperationalError:外鍵不匹配 - “project_projectpage”引用“auth_user” (django.db.utils.OperationalError: foreign key mismatch - "project_projectpage" referencing "auth_user")

如何使用 Wagtail 鉤子在 Wagtail 中生成自定義鏈接 (How to generate a custom link in Wagtail using Wagtail hooks)

Wagtail:如何設置單元測試以進行簡單的頁面編輯? (Wagtail: How to setup up unittest for simple page edit?)

如何修復錯誤“str”對像沒有屬性“relative_url” (How to fix error 'str' object has no attribute 'relative_url')

如何將帖子從 Wordpress 導入 Wagtail 2(Draftail 編輯器),包括圖像? (How to import posts from Wordpress to Wagtail 2 (Draftail editor) including images?)

如何用外鍵鏈接兩種形式(wagtail 形式和 django 形式)? (How to link two forms (wagtail form and django form) with a foreign key?)

為什麼 RichText 不能在 wagtail 管理員中為帖子工作?這是發生的事情的類型:<h2>嘗試 post.content|richtext</h2> (Why is RichText not working in wagtail admin for posts? This is the type of thing that happens: <h2>Trying post.content|richtext</h2>)

Windows 10 上 wagtail 的客戶端文件夾在哪裡 (Where is the client folder of wagtail on windows 10)

過濾從 Wagtail 核心頁面導入的多個模型的自定義字段 (Filter on custom field across multiple models that import from Wagtail core Page)







留言討論